package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.item.dto.BrandDTO;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.service
 * @date 2019/6/24 21:10
 * @description:
 **/
@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows, String sortBy, String key, Boolean desc) {

        //分页
        PageHelper.startPage(page,rows);

        //添加过滤条件
        //初始化example对象
        Example example = new Example(Brand.class);
        if(StringUtils.isNoneBlank(key)){
            //获得Criteria来设置条件
            example.createCriteria().orLike("name","%"+key+"%").orEqualTo("letter",key.toUpperCase());
        }

        if (StringUtils.isNotBlank(sortBy)){
            example.setOrderByClause(sortBy+(desc ?" DESC":" ASC"));
        }

        List<Brand> brandList = brandMapper.selectByExample(example);

        //非空判断结果
        if (CollectionUtils.isEmpty(brandList)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }


        //将结果集封装到PageInfo，可以获取总记录数和总页数
        PageInfo<Brand> brandPageInfo = new PageInfo<>(brandList);

        //转化brand集合为DTO集合
        List<BrandDTO> brandDTO = BeanHelper.copyWithCollection(brandList, BrandDTO.class);

        //返回分页结果
        return new PageResult<>(brandPageInfo.getTotal(),brandDTO);

    }

    @Transactional
    public void saveBrand(BrandDTO dto, List<Long> ids) {

        //新增品牌
        //转换dto为brand
        Brand brand = BeanHelper.copyProperties(dto, Brand.class);
        int count = brandMapper.insertSelective(brand);
        if (count!=1){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        //新增category——brand中间表数据
       count= brandMapper.insertCategoryBrand(brand.getId(),ids);
        if (count!=ids.size()){
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Transactional
    public void updateBrand(BrandDTO dto, List<Long> ids) {
        //转化dto为Brand类型
        Brand brand = BeanHelper.copyProperties(dto, Brand.class);
        //根据brand的Id更新brand对象信息
        int count = brandMapper.updateByPrimaryKeySelective(brand);
        if (count!=1){
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }


        //先删除原先的数据
        brandMapper.deleteCategoryBrand(brand.getId());
        //更新中间表格
        count = brandMapper.insertCategoryBrand(brand.getId(), ids);
        if (count!=ids.size()){
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    public Brand queryBrandByBid(Long bid){
        return brandMapper.selectByPrimaryKey(bid);
    }

    /**
     * 根据cid回显品牌信息
     * @param cid
     * @return
     */
    public List<BrandDTO> queryByCid(Long cid) {

        List<Brand> list= brandMapper.queryByCid(cid);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,BrandDTO.class);

    }


    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    public BrandDTO queryById(Long id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);
        if (brand==null){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
       return BeanHelper.copyProperties(brand,BrandDTO.class);
    }


    /**
     * 根据品牌id批量查询品牌
     * @param idList 品牌id的集合
     * @return 品牌的集合
     */
    public List<BrandDTO> queryBrandByIds(List<Long> idList) {

        List<Brand> list = brandMapper.selectByIdList(idList);
        // 判断是否为空
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, BrandDTO.class);
    }
}
